using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Voodoo.Pattern;

public class UIAudio : MonoBehaviour, ISwichSound
{
	private AudioSource audioSource;

	private bool canPlay;

	[SerializeField]
	private AudioClip _button;

	[SerializeField]
	private AudioClip _lostFuel;

	[SerializeField]
	private AudioClip _upgrade;

	[SerializeField]
	private AudioClip _win;

	[SerializeField]
	private AudioClip _coinAppear;

	[SerializeField]
	private AudioClip _buildingUpgrade;

	[SerializeField]
	private Transform[] _canvases;

	private Action _buttonSoundAction;

	private void Awake()
	{
		if ((bool)SingletonMB<GameManager>.Instance)
		{
			SingletonMB<GameManager>.Instance.UIAudio = this;
		}
		if ((bool)CityBuildingManager.Instance)
		{
			CityBuildingManager.Instance.UIAudio = this;
		}
		_buttonSoundAction = delegate
		{
			PlayButtonSound();
		};
	}

	private void Start()
	{
		audioSource = GetComponent<AudioSource>();
		audioSource.loop = false;
		audioSource.playOnAwake = false;
		List<Button> list = new List<Button>();
		Transform[] canvases = _canvases;
		foreach (Transform aParent in canvases)
		{
			list.AddRange(FinderDeepChildren.FindDeepChild<Button>(aParent, "Button"));
		}
		foreach (Button item in list)
		{
			item.onClick.AddListener(delegate
			{
				PlayButtonSound();
			});
		}
		if (Application.platform == RuntimePlatform.Android)
		{
			return;
		}
		foreach (Button item2 in list)
		{
			item2.onClick.AddListener(delegate
			{
				PlayButtonHaptic();
			});
		}
	}

	private void PlayButtonHaptic()
	{
		if ((bool)SingletonMB<GameManager>.Instance)
		{
			SingletonMB<GameManager>.Instance.GameplayHaptic.PlayHaptic(HapticType.Button);
		}
	}

	public void SoundOff()
	{
		canPlay = false;
	}

	public void SoundOn()
	{
		canPlay = true;
	}

	private void PlayButtonSound()
	{
		if (canPlay)
		{
			audioSource.PlayOneShot(_button);
		}
	}

	public void PlaySound(UISoundType soundType)
	{
		if (canPlay)
		{
			switch (soundType)
			{
			case UISoundType.Button:
				audioSource.PlayOneShot(_button);
				break;
			case UISoundType.Upgrade:
				audioSource.PlayOneShot(_upgrade);
				break;
			case UISoundType.LostFuel:
				audioSource.PlayOneShot(_lostFuel);
				break;
			case UISoundType.Win:
				audioSource.PlayOneShot(_win);
				break;
			case UISoundType.CoinAppear:
				audioSource.PlayOneShot(_coinAppear);
				break;
			case UISoundType.BuildingUpgrade:
				audioSource.PlayOneShot(_buildingUpgrade);
				break;
			}
		}
	}
}
